package com.zl.learning.io;

//import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * Created by zhaolei on 2017/6/29.
 */
public class JsonRead {

    @Test
    public void reaJson() {
        try (FileInputStream fi = new FileInputStream("conf/in.json");
             InputStreamReader ir = new InputStreamReader(fi, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(ir)) {
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line.trim());
            }
            String val = FileUtils.readFileToString(new File("conf/in.json"), "UTF-8");
            System.out.println(sb.toString());
//            JsonObject obj = new Gson().fromJson(sb.toString(),JsonObject.class);
            JsonObject obj = new Gson().fromJson(val, JsonObject.class);
            System.out.println(obj.getAsJsonObject("jsonArg").get("name").getAsString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test() {
        testArg("a", "2", "3");
        System.out.println(String.valueOf(1));
    }

    @Test
    public void test1() throws Throwable {
        MethodHandle mh = MethodHandles.lookup().findVirtual(JsonRead.class, "testArg", MethodType.methodType(void.class, String[].class));
        mh.invoke(new JsonRead(), "aa", "bb");
    }

    private void testArg(String... args) {
        System.out.println(Arrays.toString(args));
        System.out.println(String.join(":", args));
    }
}
